<?php
    session_start();
    require_once 'config.php' ;
	require_once 'Date.php';

    if(isset($_REQUEST["nickname"])) {
	    if(isset($_REQUEST['cc_id']) && empty($_REQUEST['cc_id'])) {
		    $ifDup = checkDup($_REQUEST["nickname"],(int)$_SESSION["userid"]);
		    if ($ifDup == FALSE) {
			addCredit($_REQUEST["nickname"],(int)$_SESSION["userid"]);
		    }
		    else
		    {
			echo '<span class="error">Credit card already added.</span>';
		    }
	    }
	    else {
		    $res = saveCreditCard($_REQUEST["cc_id"], $_REQUEST["nickname"], $_SESSION["userid"]);
	    }
	}
	else if(isset($_REQUEST['action'])) {
		$action = $_REQUEST['action'];
		if($action == "edit_cc") {
			$cc = getCreditCard($_REQUEST["cc"], $_SESSION["userid"]);
			$edit = true;
		}
		else if($action == "delete_cc") {
			deleteCreditCard($_REQUEST['cc'], $_SESSION['userid']);
		}
		else {
			echo '<span class="error">Invalid action.</span>';
		}
	}

	$rcredit = getCreditCardsncash($_SESSION["userid"]);

	require_once 'header.php';
?>
<br/>
<table style="width: 100%; border: 0px;" class="menu_table">
<tr>
<td colspan="3" style="text-align: center;"><b><?=ucfirst($_SESSION['username']);?>'s Options</b></td>
</tr>
</table>
<br/><br/>
<div style="border: 1px solid #cccccc;">
<table style="width: 100%;">
<tr><td style="border: none;"><b>Credit Card Options</b></td></tr>
</table>
<form name="addcredit" action="options.php" method="post">
<input type="hidden" name="cc_id" value="<?= $cc['cc_id'] ?>"/>
<table class="menu_table">
<tr>
	<td><b>Add/Edit a Credit Card: </b></td>
	<td>Credit Card NickName:&nbsp;</td>
	<td><input type="text" name="nickname" id="idnickname" value="<?= $edit ? $cc['cc_nickname'] : '' ?>" style="width: 250px;"/></td>
   <td><input type="submit" name="creditadd" id="idccadd" value="Save"/></td>
</tr>
</table>
<br/>
  <table style="width: 100%;">
        <tbody id="ccards">
        <tr>
		<th>&nbsp;</th>
            <th>Credit Cards</th>
            <th colspan="2">Actions</th>
       </tr>
<?php
	if(is_array($rcredit)) {
		$i = 0;
		foreach($rcredit as $data) { 
?>
	<tr>
	    <td><?= ++$i ?></td>
            <td><?=$data["cc_nickname"] ?></td>
	    <td><a href="options.php?action=edit_cc&cc=<?= $data["cc_id"] ?>">Edit</a></td>
	    <td><a href="options.php?action=delete_cc&cc=<?= $data["cc_id"] ?>">Delete</a></td>
<?php
		} 
	}
?>

        </tr>
        </tbody>
    </table>
</div>
</body>
</html>
